local skill = fk.CreateSkill {
  name = "shendao_twelve_chapters_skill",
}

Fk:loadTranslationTable{
  ["shendao_twelve_chapters_skill"] = "法经",
}

skill:addEffect("cardskill", {
  can_use = Util.FalseFunc,
})

skill:addEffect(fk.AfterCardsMove, {
  priority = 0,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if player.room:getBanner("hanqing__chushi") and
      player.room:getCardArea(player.room:getBanner("hanqing__chushi")[1]) == Card.Void then
      for _, move in ipairs(data) do
        if move.from and move.toArea == Card.Void then
          if move.from == player and not player.dead then
            for _, info in ipairs(move.moveInfo) do
              if info.fromArea == Card.PlayerHand and
                Fk:getCardById(info.cardId).name == "shendao_twelve_chapters" then
                return true
              end
            end
          end
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = 0
    if table.every(room.alive_players, function (p)
      return p.hp <= player.hp
    end) then
      n = n + 1
    end
    if table.every(room.alive_players, function (p)
      return p:getHandcardNum() <= player:getHandcardNum()
    end) then
      n = n + 1
    end
    if table.every(room.alive_players, function (p)
      return #p:getCardIds("e") <= #player:getCardIds("e")
    end) then
      n = n + 1
    end
    if n > 0 then
      player:drawCards(n, skill.name)
    else
      room:loseHp(player, 1, skill.name)
    end
  end,
})

skill:addEffect(fk.BeforeCardsMove, {
  can_refresh = function (self, event, target, player, data)
    for _, move in ipairs(data) do
      if move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerHand and
            Fk:getCardById(info.cardId).name == "shendao_twelve_chapters" then
            return true
          end
        end
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local new_data = {}
    for _, move in ipairs(data) do
      local new_move = {}
      if #move.moveInfo > 0 and move.toArea ~= Card.Void then
        local orig_info, new_info = {}, {}
        for i = 1, #move.moveInfo do
          local info = move.moveInfo[i]
          local will_destruct = false
          if info.fromArea == Card.PlayerHand and Fk:getCardById(info.cardId).name == "shendao_twelve_chapters" then
            will_destruct = true
          end
          if will_destruct then
            table.insert(new_info, info)
          else
            table.insert(orig_info, info)
          end
        end
        move.moveInfo = orig_info
        if #new_info > 0 then
          new_move = {
            moveInfo = new_info,
            from = player,
            to = nil,
            toArea = Card.Void,
            moveReason = fk.ReasonJustMove,
            proposer = nil,
            skillName = skill.name,
            moveVisible = true,
          }
        end
      end
      if next(new_move) then
        table.insert(new_data, new_move)
      end
    end
    if next(new_data) then
      table.insertTable(data, new_data)
    end
  end,
})

return skill
